﻿using MvvmFoundation.Wpf;
using ServiceOperater.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ServiceOperater
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private Dictionary<ObservableObject, Window> winDic = new Dictionary<ObservableObject, Window>();
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MainViewModel();

            RegisterMessenger();
        }

        private void RegisterMessenger()
        {
            App.Messenger.Register<String>(MessengerToken.DIALOG_SHOW, (msg) =>
            {
                MessageBox.Show(msg);
            });

            App.Messenger.Register<ObservableObject>(MessengerToken.WINDOW_SHOW, (viewMode) =>
            {
                var vmName = viewMode.GetType().Name;
                if (!vmName.EndsWith("ViewModel"))
                {
                    return;
                }

                var viewName = vmName.Substring(0, vmName.IndexOf("Model"));
                Window viewWin = (Window)Type.GetType("ServiceOperater.Views." + viewName).GetConstructor(Type.EmptyTypes).Invoke(null);
                viewWin.DataContext = viewMode;

                winDic.Add(viewMode, viewWin);

                viewWin.ShowDialog();
            });

            App.Messenger.Register<ObservableObject>(MessengerToken.WINDOW_CLOSE, (viewMode) =>
            {
                var viewWin = winDic[viewMode];
                if (null == viewWin)
                {
                    return;
                }

                winDic.Remove(viewMode);
                viewWin.Close();
            });
        }

        //private void MessageBox()
        //{
        //    string sMessageBoxText = "Do you want to continue?";
        //    string sCaption = "My Test Application";

        //    MessageBoxButton btnMessageBox = MessageBoxButton.YesNoCancel;
        //    MessageBoxImage icnMessageBox = MessageBoxImage.Warning;

        //    MessageBoxResult rsltMessageBox = MessageBox.Show(sMessageBoxText, sCaption, btnMessageBox, icnMessageBox);

        //    switch (rsltMessageBox)
        //    {
        //        case MessageBoxResult.Yes:
        //            /* ... */
        //            break;

        //        case MessageBoxResult.No:
        //            /* ... */
        //            break;

        //        case MessageBoxResult.Cancel:
        //            /* ... */
        //            break;
        //    }
        //}
    }
}
